// pages/signin/signin.js
Page({
  data:{ 
      account:'',
      pwd:'',
      pwdvisible:false,
      modalText:'',
      btnText:'获取验证码',
      validing: false,
      timer: '',
      pwdfocus:false,
      hidden:true
  },
  confirm(){
    this.setData({
      hidden:true
    })
  },
  lookpwd(){
    this.setData({
      pwdvisible:!this.data.pwdvisible
    })
  },
  accountinput(e){
    this.setData({
      account:e.detail.value
    })
  },
  pwdtinput(e){
    this.setData({
      pwd:e.detail.value
    })
  },
  getValid(){
    var self = this;
    if(self.data.account.length!=11){
      if(self.data.account.lenght==0){
        self.setData({modalText:'请输入手机号格',hidden:false})
      }else
      self.setData({modalText:'手机号格式不正确',hidden:false})
      return;
    }
    if (self.mydata.requesting) return;
    self.mydata.requesting = true; 
    self.countDown();
    getApp().request({
      url: 'send_msg_code', 
      data: {
        mobile: self.data.account 
      },
      success: function(res) {
          if(!res.data.flag){self.setData({
              modalText:res.data.msg,
              hidden:false,
              btnText: '重新获取',
              validing: false
            })
          }
      },fail(){
        wx.showModal({
            content: '请求失败',
            showCancel:false,
            success: function(res) {
            }
        })
        self.setData({
          title: "获取验证码失败",
          hidden: false,
          btnText: '重新获取',
          validing: false
        })
      },complete(){
      }
    })
  },
  mydata: {
    requesting: false
  },
  countDown(){
    var self = this;
    var time = 60;
    var timer = setInterval(function(){
        if(time<0){
            clearInterval(timer);
             self.setData({
                btnText:'重新获取',
                validing:false
            })
            self.mydata.requesting = false;
            return;
        }else{
            self.setData({
                btnText:time,
                validing:true
            })
            time--;
        }
    },1000)
  },confirm(){
    this.setData({hidden:true})
  },next(){
    var self = this;
      if(self.data.pwd.length==0){
        self.setData({
            title:"请输入验证码",
            hidden:false
        })
        return;
        }
      getApp().request({
      url: 'varify_msg_code', 
      data: {
        code: self.data.pwd 
      },
      success(res) {
        if(res.data.flag){
            wx.redirectTo({url: '../resetpwd/resetpwd?mobile='+self.data.account})
        }else{
            self.setData({
              title:res.data.msg,
              hidden:false
          })
        }
      },fail(){
          wx.showModal({
              content: '请求失败',
              showCancel:false,
              success: function(res) {
              }
          })
      }
    })
    
  }
})